package com.crm.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.jdao.ISalesOpportunityDAO;
import com.crm.model.jdao.IUserDAO;
import com.crm.model.mdao.PowerandroleMapper;
import com.crm.model.mdao.RoleandusersMapper;
import com.crm.model.mdao.UserMapper;
import com.crm.model.mpojo.Roleandusers;
import com.crm.model.mpojo.Users;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Roleanduser;
import com.crm.model.pojo.SalesOpportunity;
import com.crm.model.pojo.User;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

@Service

public class UserServices {

	@Autowired
	private IUserDAO userDAO;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleandusersMapper roleandusersMapper;
	
	private Map<String,String> errMap=new HashMap<>();
	
	/**
	 * 分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(Integer pageNum, Integer pageSize,Integer daptid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Map<String, String>> list = userMapper.selectAllTable(daptid,search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 要查找该用户下的员工,没有则查找当前登录用户,暂时采用模糊查询
	 */
	public List<User> findByUNameLike(String uName){
		return userDAO.findByUNameLike(uName);	
	}

	/**
	 * 要查找该用户下的员工姓名,没有则查找当前登录用户,暂时采用查找所有
	 */
	public List<String> findAllusername(){
		return userMapper.selectAllUsername();
	}
	 /**
	  * 根据客户名称查询
	  * @param custName
	  * @return
	  */
	public Users findByUserName(String userName) {
		return userMapper.findByUsername(userName);
		
	}
	/**
	 *将list的数据转换成JsonArray的数据
	 * @return
	 */
	public JSONArray ListTransJSONArray(List<User> list) {
		
		JSONArray array =new JSONArray();

		for (User user : list) {
			JSONObject obj = new JSONObject();			
			obj.put("userId",user.getUId());
			obj.put("value",  user.getUName());
			array.add(obj);

		}
		return array;	
	}
	/**
	 * 使用jpa分页查询
	 * 
	 * @return
	 */
	public List<User> findAll() {
		//这里要写成带条件的分页！！！！！！！！！！！！
		Pageable pageable =PageRequest.of(0,5);	
		//查询条件构造
		 Specification<User> spec=new Specification<User>(){

			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				//Predicate p=cb.equal(root.get("status").as(Integer.class), 1206);
				Predicate p=null;
				return p;
			}
			 
		 };
		Page<User> page =userDAO.findAll(spec,pageable);
		return page.getContent();

	}
	
	/**
	 * 根据用户id查询
	 * @param uName
	 * @return
	 */
	public User findByUId(Integer uId) {
		return userDAO.findByUId(uId);
	}
	
	/**
	 * 查询所有的员工信息，用于前台筛选
	 * @return
	 */
	public List<Map<String, String>> selectUserAll() {
		return userMapper.selectUserAll();
	}
	
	/**
	 * 查询某部门所有的员工信息
	 * @return
	 */
	public List<Map<String, String>> selectWorkerAll(Integer deptId) {
		return userMapper.selectWorkerAll(deptId);
	}
	
	/**
	 * 用户登录验证
	 * @param username
	 * @param password
	 * @param session
	 * @return 
	 */
	public int loginUser(String username, String password, HttpSession session) {
		if(username.trim()==null||username.trim()=="") {
			errMap.clear();
			errMap.put("username", "用户名不能为空");
			return 0;
		}
		if(password.trim()==null||password.trim()=="") {
			errMap.clear();
			errMap.put("password", "密码不能为空");
			return 0;
		}
		Users user = userMapper.findByUsername(username);
		if(user!=null) {
			if(user.getuPassword().equals(password.trim())) {
				LoginUtil.setUser(user);
				session.setAttribute("user", user);
				return 1;
			}else {
				errMap.clear();
				errMap.put("password", "密码不正确");
			}
		}else {
			errMap.clear();
			errMap.put("username", "用户名不存在");
		}
		session.setAttribute("errMap", errMap);
		return 0;
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @param limap 
	 * @return
	 */
	public String save(User user, List<Integer> limap) {
		user.setUPassword("123456");
		user.setuDate(new Date());
		user.setUState(1367);
		user.setuHeadimg("../../../upload/20190307153535QQ截图20190307092036.png");
		user.setuStatu("1");
		User save = userDAO.save(user);
		for (Integer integer : limap) {
			if(integer==null) {
				continue;
			}
			Roleandusers Roleandusers=new Roleandusers();
			Roleandusers.setuId(save.getUId());
			Roleandusers.setRoleId(integer);
			roleandusersMapper.insertSelective(Roleandusers);
		}
		return "";
	}
	/**
	 * 修改部门信息
	 * @param user
	 * @param limap 
	 * @return
	 */
	public String updateUser(Users user, List<Integer> limap) {
		userMapper.updateByPrimaryKeySelective(user);
		//删除用户下的所有角色
		roleandusersMapper.deleteByUId(user.getuId());
		for (Integer integer : limap) {
			if(integer==null) {
				continue;	
			}
			Roleandusers Roleandusers=new Roleandusers();
			Roleandusers.setuId(user.getuId());
			Roleandusers.setRoleId(integer);
			roleandusersMapper.insertSelective(Roleandusers);
		}
		return "";
	}
	
	/**
	 * 通过userid查询用户信息
	 * @param userId
	 * @return
	 */
	public JSONObject findByone(Integer userId) {
		User u=userDAO.findByUId(userId);
		return ToJSONObject(u) ;
		
	}
	
	private JSONObject ToJSONObject(User u) {
		JSONObject json=new JSONObject();
		json.put("userName", u.getUName());
		return json;
		
	}
	
	/**
	 * 修改部门信息
	 * @param user
	 * @param limap 
	 * @return
	 */
	public String updateUserinfo(Users user) {
		userMapper.updateByPrimaryKeySelective(user);
		return "";
	}
	
	
	
}
